*******************************Graph Generator**********************************
clear
set more off


cd "...\Replication_Files" // Set the directory of input and output

*Figure 1
use "Data\Production_Scope_byPatent_Census.dta",clear 
twoway (scatter naics12_num year if ever_u_patent==1 & year>=1978 & year~=2002, connect(l) yaxis(1) ylabel(2(0.2)3, axis(1) nogrid) msymbol(O))(scatter naics12_num year if ever_u_patent==0 & year>=1978 & year~=2002, connect(l) yaxis(2) lpattern(dash) ylabel(1(0.01)1.05, axis(2) nogrid) msymbol(d)), text(2.17 2001 "Innovating Firms", color(midblue) size(medium))text(2.5 2001 "Others", color(cranberry) size(medium))legend(off) xtitle("Year") ytitle("Number of Industries (Innovating)", axis(1)) ytitle("Number of Industries (Others)",axis(2))graphregion(color(white))xlabel(,nogrid) bgcolor(white)
graph export "Figures\Figure1.eps", as(eps) name("Graph") preview(on) replace

*Figure 2(a)
import excel "Data\RD_Sale_by_Size(no_impute).xlsx", sheet("Sheet1") firstrow clear //Import raw data
//Define R&D expense-to-sales ratio
gen rd1000p=rd1000_4999+rd5000p  
gen sale1000p=sale1000_4999+sale5000p
gen ratio_large_rest= rd1000p/rd0_999
 
gen rd_sale=rd_total/sale_total
gen rd_sale0_999=rd0_999/sale0_999
gen rd_sale1000_4999=rd1000_4999/sale1000_4999
gen rd_sale5000p=rd5000p/sale5000p
gen rd_sale1000p=rd1000p/sale1000p

twoway scatter ratio_large_rest year, connect(l) xtitle("Year") ytitle("R&D Ratio, Large Firms/Others") xlabel(,nogrid) ylabel(,nogrid) graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure2a.eps", as(eps) name("Graph") preview(on) replace

*Figure 2(b)
twoway (scatter rd_sale0_999 year, connect(l) msymbol(O))(scatter rd_sale1000p year, connect(l) msymbol(d) lpattern(dash)), text(0.08 2007 "Small/Medium", color(midblue) size(medium)) text(0.038 2007 "Large",color(cranberry) size(medium)) legend(off) xtitle("Year") ytitle("R&D Intensity by Size") xlabel(,nogrid) ylabel(,nogrid) graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure2b.eps", as(eps) name("Graph") preview(on) replace

*Figure 3(a)
use "Data\Patent_Traded_Share.dta",clear
twoway (scatter reass_share_1 year if year>=1983 & year<=2004, connect(l) msymbol(O))(scatter reass_share_2 year if year>=1983 & year<=2004, connect(l) msymbol(d) lpattern(dash))(scatter reass_share_3 year if year>=1983 & year<=2004, connect(l) msymbol(t) lpattern(shortdash)) (scatter reass_share_4 year if year>=1983 & year<=2004, connect(l) msymbol(s) lpattern(dash_dot)), text(0.14 2003 "-4~0 Years", color(midblue) size(medium))text(0.11 2003 "1~5 Years", color(cranberry) size(medium))text(0.085 2003 "6~10 Years", color(midgreen) size(medium))text(0.04 2003 ">10 Years",color(gold) size(medium)) legend(off) xtitle("Grant Year") ytitle("Traded Share (Citation-Weighted)") xlabel(,nogrid) ylabel(0(0.05)0.15, nogrid) graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure3a.eps", as(eps) name("Graph") preview(on) replace

*Figure 3(b)
use "Data\Match_RD_Production.dta",clear
twoway scatter within_scope_ratio1 year, connect(l) xtitle("Year") ytitle("Matching Rate between Innovation and Production") xlabel(1980[5]2000) xlabel(, nogrid) ylabel(, nogrid) graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure3b.eps", as(eps) name("Graph") preview(on) replace

*Figure 5
import excel "Data\Basic_Applied_Development.xlsx", sheet("Table 55") firstrow clear //Import raw data
destring Year, replace

gen basic_applied= BasicResearchConstant/( BasicResearchConstant+ AppliedResearchConstant ) //Define the share of research costs on basic research
gen research_share= ( BasicResearchConstant+ AppliedResearchConstant )/ AllRDConstant //Define the share of R&D costs on research

twoway (scatter research_share Year if Year<=1997, connect(l)msymbol(o))(scatter basic_applied Year if Year<=1997, connect(l)msymbol(d) lpattern(dash)), text(0.28 1995 "R/R&D", color(navy) size(medium)) text(0.17 1995 "Basic R/R",color(maroon) size(medium)) legend(off) xtitle("Year") ytitle("Share") xlabel(,nogrid) ylabel(,nogrid) graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure5.eps", as(eps) name("Graph") preview(on) replace

*Figure 6(a)
use "Data\Production_Scope_byPatent_FixedEmp.dta",clear
twoway (scatter ind_num20 year if ever_u_patent20==1 & year>=1978 & year~=2002, connect(l) msymbol(O))(scatter ind_num20 year if ever_u_patent20==0 & year>=1978 & year~=2002, connect(l) msymbol(d) lpattern(dash)), text(2.05 2001 "Innovating Firms", color(midblue) size(medium))text(1.15 2001 "Others", color(cranberry) size(medium))legend(off) xtitle("Year") xlabel(,nogrid) ylabel(,nogrid) ytitle("Number of Industries", color (black)) graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure6a.eps", as(eps) name("Graph") preview(on) replace

*Figure 6(b)
twoway (scatter ind_num1000 year if ever_u_patent1000==1 & year>=1978 & year~=2002, connect(l) msymbol(O))(scatter ind_num1000 year if ever_u_patent1000==0 & year>=1978 & year~=2002, connect(l) msymbol(d) lpattern(dash)), text(2.35 2001 "Innovating Firms", color(midblue) size(medium))text(1.4 2001 "Others", color(cranberry) size(medium))legend(off) xtitle("Year") xlabel(,nogrid) ylabel(,nogrid) ytitle("Number of Industries", color (black)) graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure6b.eps", as(eps) name("Graph") preview(on) replace

*Figure 7
use "Data\Patent_Employee.dta",clear
twoway (scatter patent_cite_emp year if large_firm==1 & year<=2002, connect(l) msymbol(O))(scatter patent_cite_emp year if large_firm==0 & year<=2002, connect(l) msymbol(D) lpattern(dash)), text(0.04 1994 "Large", color(midblue) size(medium))text(0.12 1994 "Small/Medium", color(cranberry) size(medium)) legend(off) xtitle("Year") ytitle("Patents per Employee (Citation Weighted)") xlabel(1975(5)2005, nogrid)ylabel(, nogrid) graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure7.eps", as(eps) name("Graph") preview(on) replace

* Figure 8
use "Data\RD_Sale.dta",clear
//Define industry
rename sic sic_seg_main
destring sic_seg_main,replace
	drop if sic_seg_main<2000
	gen industry = 4 if sic_seg_main>=6000 & sic_seg_main<=8999
	replace industry = 3 if sic_seg_main>=5000 & sic_seg_main<=5999
	replace industry = 2 if sic_seg_main>=4000 & sic_seg_main<=4999
	replace industry = 1 if sic_seg_main>=2000 & sic_seg_main<=3999
	label define industry_lb 1 "Manufacturing" 2 "Utilities and Transportation" 3 "Wholesale & Retail Trade" 4 "Services" 
	label values industry industry_lb
keep if rd_indicator==1

// Calculate R&D expense-to-sales ratio for large and small firms by industry
gen firm_num=1
collapse (sum) xrd sale firm_num (mean) rd_ratio, by (large industry year)
gen rd_ratio2= xrd/ sale

drop if large==.
drop if year==2023 | year==.

twoway (scatter rd_ratio2 year if large==1 & year>=1975 & year<=2006, connect(l) msymbol(O) xlabel(, nogrid) ylabel(, nogrid) msymbol(O))(scatter rd_ratio2 year if large==0 & year>=1975 & year<=2006, connect(l) msymbol(D) lpattern(dash) xlabel(1975(10)2010, nogrid) xsc(r(1975(10)2010)) ylabel(, nogrid) msymbol(d)), by(industry)xtitle("Year") ytitle("R&D-Expense-to-Total-Sales Ratio")  legend(label(1 "Large Firms") label(2 "Small Firms")) graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure8.eps", as(eps) name("Graph") preview(on) replace

*Figure 9

use "Data\RD_Sale.dta",clear
rename sic sic_seg_main
destring sic_seg_main,replace
	drop if sic_seg_main<2000
	gen industry = 1 if sic_seg_main>=2000 & sic_seg_main<=3999
	replace industry = 2 if sic_seg_main>=4000 & sic_seg_main<=4999
	replace industry = 3 if sic_seg_main>=5000 & sic_seg_main<=5999
	replace industry = 4 if sic_seg_main>=6000 & sic_seg_main<=8999
	label define industry_lb 1 "Manufacturing" 2 "Utilities and Transportation" 3 "Wholesale & Retail Trade" 4 "Services" 
	label values industry industry_lb
keep if rd_indicator==1
drop if industry==.

//Define firm size group

egen emp_10=pctile(emp), p(10)
egen emp_25=pctile(emp), p(25)
egen emp_50=pctile(emp), p(50)
egen emp_75=pctile(emp), p(75)
egen emp_90=pctile(emp), p(90)

gen size_emp=1 if emp<=emp_10
replace size_emp=2 if emp>=emp_10 & emp<emp_25
replace size_emp=3 if emp>=emp_25 & emp<emp_50
replace size_emp=4 if emp>=emp_50  & emp<emp_75
replace size_emp=5 if emp>=emp_75  & emp<emp_90
replace size_emp=6 if emp>=emp_90  & emp~=.


label define size_lb 1 "Below 10%" 2 "Above 10%, Below 25%" 3 "Above 25%, Below 50%" 4 "Above 50%, Below 75%" 5"Above 75%, Below 90%" 6"Above 90%"
label values size_emp size_lb

// Calculate R&D expense-to-sales ratio by firm size group
gen firm_num=1
collapse (sum) xrd_sum=xrd sale firm_num (mean) xrd_mean=xrd rd_ratio_mean=rd_ratio (max) rd_ratio_max=rd_ratio (min) rd_ratio_min=rd_ratio (sd) rd_ratio_sd=rd_ratio, by (size_emp year)
gen rd_ratio2= xrd_sum/ sale

drop if size_emp==.
drop if year==2023 | year==.
drop if year<1972

twoway (scatter rd_ratio2 year if year>=1975 & year<=2006 & size_emp==1, connect(l) msymbol(O))(scatter rd_ratio2 year if year>=1975 & year<=2006 & size_emp==2, connect(l) msymbol(D)lpattern(dash))(scatter rd_ratio2 year if year>=1975 & year<=2006 & size_emp==3, connect(l) msymbol(T)lpattern(longdash))(scatter rd_ratio2 year if year>=1975 & year<=2006 & size_emp==4, connect(l) msymbol(S)lpattern(shortdash))(scatter rd_ratio2 year if year>=1975 & year<=2006 & size_emp==5, connect(l) msymbol(+)lpattern(dash_dot))(scatter rd_ratio2 year if year>=1975 & year<=2006 & size_emp==6, connect(l) msymbol(Oh)lpattern(dash_dot_dot)), xtitle("Year") ytitle("R&D-Expense-to-Total-Sales Ratio") xlabel(1975(5)2010, nogrid) xsc(r(1975(5)2010)) ylabel(, nogrid) graphregion(color(white)) bgcolor(white) legend(label(1 "<10%") label(2 "10%-25%") label(3 "25%-50%") label(4 "50%-75%") label(5 "75%-90%") label(6 ">=90%")) 

graph export "Figures\Figure9.eps", as(eps) name("Graph") preview(on) replace

*Figure 10
import excel "Data\UGAPatentLitigationDataFileMaster1March2013.xlsx", sheet("Sheet1") firstrow clear //Import raw data
drop BL-CZ

gen DCyear=substr(DistDate,-4,.)
replace DCyear="" if DistDate=="unknown"
destring DCyear,replace

replace DCState="" if DCState=="n/a" | DCState=="unknown" 

// Identify invalidated cases at the district court level
gen DC_INV=1 if DCDecision=="INV"
replace DC_INV=0 if DCDecision=="NINF" | DCDecision=="V&I"

gen DC_VI=1 if DCDecision=="V&I"
replace DC_VI=0 if DCDecision=="NINF" | DCDecision=="INV"

gen DC_INF=1 if DCDecision=="V&I"
replace DC_INF=0 if DCDecision=="NINF"

gen n=1

collapse (mean) DC_INV DC_VI DC_INF (sum) n, by (DCyear)
twoway scatter DC_INV DCyear if DCyear>=1940 & DCyear<=2002, connect(l) xlabel(,nogrid) ylabel(0[0.1]0.8, nogrid) xline(1982) text(0.67 1985 "CAFC", color(red) size(medium)) xtitle("Year") ytitle("Invalidation Rate") graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure10.eps", as(eps) name("Graph") preview(on) replace

*Figure 11(a)
use "Data\Patent_Traded_Share.dta",clear
twoway (scatter reass_cite_share_1 year if year>=1983 & year<=2004, connect(l) msymbol(O))(scatter reass_cite_share_2 year if year>=1983 & year<=2004, connect(l) msymbol(d) lpattern(dash))(scatter reass_cite_share_3 year if year>=1983 & year<=2004, connect(l) msymbol(t) lpattern(shortdash)) (scatter reass_cite_share_4 year if year>=1983 & year<=2004, connect(l) msymbol(s) lpattern(dash_dot)), text(0.167 2003 "-4~0 Years", color(midblue) size(medium))text(0.123 2003 "1~5 Years", color(cranberry) size(medium))text(0.086 2003 "6~10 Years", color(midgreen) size(medium))text(0.05 2003 ">10 Years",color(gold) size(medium)) legend(off) xtitle("Grant Year") ytitle("Traded Share (Citation-Weighted)") xlabel(,nogrid) ylabel(0(0.05)0.2, nogrid)graphregion(color(white)) bgcolor(white)
graph export "Figures\Figure11a.eps", as(eps) name("Graph") preview(on) replace

*Figure 11(b)
clear
run "Codes\Reassign_Ratio_by_Assigntype.do"
use "Data\Reassign_Ratio_by_Assigntype.dta",clear
twoway (scatter reass_grt_ratio year if year>=1981 & year<=2006 & convey_ty_new=="assignment", connect(l) yaxis(1) ytitle("Traded Share (by Sales)",axis(1)) msymbol(O))(scatter reass_grt_ratio year if year>=1981 & year<=2006 & convey_ty_new=="merger", connect(l) yaxis(2) ytitle("Traded Share (by M&As)",axis(2)) msymbol(D) lpattern(dash)), xtitle("Grant Year") legend(off) xlabel(,nogrid) ylabel(,nogrid)graphregion(color(white)) bgcolor(white) text(0.255 2005 "M&As", color(cranberry) size(medium))text(0.3 2005 "Sales", color(midblue) size(medium))

graph export "Figures\Figure11b.eps", as(eps) name("Graph") preview(on) replace



